﻿using Microsoft.Phone.Shell;
using System;
using System.Windows;
using System.Windows.Input;
namespace Omnia.UX
{
    public class AppBarMenuItem : FrameworkElement, IApplicationBarMenuItem
    {
        protected IApplicationBarMenuItem _appBarItem;
        public virtual IApplicationBarMenuItem ApplicationBarItem
        {
            get
            {
                if (_appBarItem == null)
                {
                    _appBarItem = new ApplicationBarMenuItem();
                    if (!string.IsNullOrEmpty(Text))
                        _appBarItem.Text = Text;
                    _appBarItem.Click += appBarItem_Click;
                }
                return _appBarItem;
            }
        }

        #region - IsEnabled -
        public static readonly DependencyProperty IsEnabledProperty =
        DependencyProperty.Register("IsEnabled",
        typeof(bool),
        typeof(AppBarMenuItem),
        new PropertyMetadata(true, new PropertyChangedCallback(AppBarMenuItem.OnIsEnabledPropertyChanged)));

        public bool IsEnabled
        {
            get
            {
                return (bool)base.GetValue(IsEnabledProperty);
            }
            set
            {
                base.SetValue(IsEnabledProperty, value);
            }
        }

        private static void OnIsEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as AppBarMenuItem).ApplicationBarItem.IsEnabled = (bool)e.NewValue;
        }
        #endregion

        #region - Text -
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text",
            typeof(string),
            typeof(AppBarMenuItem),
            new PropertyMetadata("", new PropertyChangedCallback(AppBarMenuItem.OnTextPropertyChanged)));

        public string Text
        {
            get
            {
                return (string)base.GetValue(TextProperty);
            }
            set
            {
                base.SetValue(TextProperty, value);
            }
        }

        private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            string text = e.NewValue as string;
            if (!string.IsNullOrEmpty(text) && (d as AppBarMenuItem)._appBarItem != null)
                (d as AppBarMenuItem)._appBarItem.Text = text;
        }
        #endregion
     
        #region - Command&CommandParameter -
        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(AppBarMenuItem), null);
        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(AppBarMenuItem), null);

        public ICommand Command
        {
            get
            {
                return (ICommand)base.GetValue(CommandProperty);
            }
            set
            {
                base.SetValue(CommandProperty, value);
            }
        }

        public object CommandParameter
        {
            get
            {
                return base.GetValue(CommandParameterProperty);
            }
            set
            {
                base.SetValue(CommandParameterProperty, value);
            }
        }
        #endregion

        public event EventHandler Click;

        protected void appBarItem_Click(object sender, EventArgs e)
        {
            if (Click != null)
                Click(sender, e);

            if (Command != null)
            {
                if (Command.CanExecute(CommandParameter))
                {
                    Command.Execute(CommandParameter);
                }
            }
          
        }
    }
}
